package com.ms.dsq_english.wall

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.ms.dsq_english.R
import com.ms.dsq_english.adapter.RVAdapterAreaCurrentAll
import com.ms.dsq_english.http.HttpManager
import com.ms.dsq_english.http.HttpManager.getRetrofitInterface
import com.ms.dsq_english.model.AllBean
import com.ms.dsq_english.utils.LogUtils
import com.ms.dsq_english.utils.MapUtilsParamNull
import com.ms.dsq_english.utils.ResultCheckUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.frag_all.*
import java.util.*

/**
 * @Description:
 * @Author: zheng
 */
class AllFragment : Fragment() {

    private lateinit var mContext: Context
    private val list: MutableList<AllBean.DataBean.SalesRegionBean> = ArrayList()
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
    {
        mContext = context!!

        return inflater.inflate(R.layout.frag_all, container, false)
    }

    override fun onResume() {
        super.onResume()
        Glide.with(mContext).load(R.drawable.img_all_up).into(all_img)
        sales
    }

    fun refresh() {
        sales
    }

    @get:SuppressLint("CheckResult")
    private val sales: Unit
        private get() {

            val params: HashMap<String, String> = MapUtilsParamNull()
                    .getBaseMap(mContext)

        getRetrofitInterface()
                    .getRegion(params)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe({ result: AllBean ->
                        if (result.code == 10000) {

                            initRecyclerViewCurrent(result.data.sale_detail)
                            //总
                            var dataItem = result.data
                            all_l1.text = dataItem.total_data.today_prestige_goods
                            all_l2.text = dataItem.total_data.today_gold_goods
                            all_l3.text = dataItem.total_data.today_order_amount
                            all_l4.text = dataItem.total_data.month_order_amount
                            all_l5.text = dataItem.total_data.shop_number
                            all_l6.text = dataItem.total_data.three_shop_number
                            all_l7.text = dataItem.total_data.prestige_goods
                            all_l8.text = dataItem.total_data.gold_goods
                            //东区
                            alld1.text = dataItem.zone_east.prestige_goods
                            alld2.text = dataItem.zone_east.gold_goods
                            alld3.text = dataItem.zone_east.today_gold_amount
                            alld4.text = dataItem.zone_east.today_prestige_amount
                            alld5.text = dataItem.zone_east.three_shop_number
                            alld6.text = dataItem.zone_east.shop_number
                            //南区
                            alln1.text = dataItem.zone_south.prestige_goods
                            alln2.text = dataItem.zone_south.gold_goods
                            alln3.text = dataItem.zone_south.today_gold_amount
                            alln4.text = dataItem.zone_south.today_prestige_amount
                            alln5.text = dataItem.zone_south.three_shop_number
                            alln6.text = dataItem.zone_south.shop_number
                            //西区
                            allx1.text = dataItem.zone_west.prestige_goods
                            allx2.text = dataItem.zone_west.gold_goods
                            allx3.text = dataItem.zone_west.today_gold_amount
                            allx4.text = dataItem.zone_west.today_prestige_amount
                            allx5.text = dataItem.zone_west.three_shop_number
                            allx6.text = dataItem.zone_west.shop_number
                            //北区
                            allb1.text = dataItem.zone_north.prestige_goods
                            allb2.text = dataItem.zone_north.gold_goods
                            allb3.text = dataItem.zone_north.today_gold_amount
                            allb4.text = dataItem.zone_north.today_prestige_amount
                            allb5.text = dataItem.zone_north.three_shop_number
                            allb6.text = dataItem.zone_north.shop_number
                            //省
                            if (dataItem.sale_province.size==0){
                                alls1.text = ""
                                ally1.text = ""
                                alls2.text = ""
                                ally2.text = ""
                                alls3.text = ""
                                ally3.text = ""
                                alls4.text = ""
                                ally4.text = ""
                                alls5.text = ""
                                ally5.text = ""
                            }
                            if (dataItem.sale_province.size==1){
                                alls1.text = dataItem.sale_province.get(0).region_province
                                ally1.text = dataItem.sale_province.get(0).amount+"千元"
                                alls2.text = ""
                                ally2.text = ""
                                alls3.text = ""
                                ally3.text = ""
                                alls4.text = ""
                                ally4.text = ""
                                alls5.text = ""
                                ally5.text = ""
                            }
                            if (dataItem.sale_province.size==2){
                                alls1.text = dataItem.sale_province.get(0).region_province
                                ally1.text = dataItem.sale_province.get(0).amount+"千元"
                                alls2.text = dataItem.sale_province.get(1).region_province
                                ally2.text = dataItem.sale_province.get(1).amount+"千元"
                                alls3.text = ""
                                ally3.text = ""
                                alls4.text = ""
                                ally4.text = ""
                                alls5.text = ""
                                ally5.text = ""
                            }
                            if (dataItem.sale_province.size==3){
                                alls1.text = dataItem.sale_province.get(0).region_province
                                ally1.text = dataItem.sale_province.get(0).amount+"千元"
                                alls2.text = dataItem.sale_province.get(1).region_province
                                ally2.text = dataItem.sale_province.get(1).amount+"千元"
                                alls3.text = dataItem.sale_province.get(2).region_province
                                ally3.text = dataItem.sale_province.get(2).amount+"千元"
                                alls4.text = ""
                                ally4.text = ""
                                alls5.text = ""
                                ally5.text = ""
                            }
                            if (dataItem.sale_province.size==4){
                                alls1.text = dataItem.sale_province.get(0).region_province
                                ally1.text = dataItem.sale_province.get(0).amount+"千元"
                                alls2.text = dataItem.sale_province.get(1).region_province
                                ally2.text = dataItem.sale_province.get(1).amount+"千元"
                                alls3.text = dataItem.sale_province.get(2).region_province
                                ally3.text = dataItem.sale_province.get(2).amount+"千元"
                                alls4.text = dataItem.sale_province.get(3).region_province
                                ally4.text = dataItem.sale_province.get(3).amount+"千元"
                                alls5.text = ""
                                ally5.text = ""
                            }
                            if (dataItem.sale_province.size==5){
                                alls1.text = dataItem.sale_province.get(0).region_province
                                ally1.text = dataItem.sale_province.get(0).amount+"千元"
                                alls2.text = dataItem.sale_province.get(1).region_province
                                ally2.text = dataItem.sale_province.get(1).amount+"千元"
                                alls3.text = dataItem.sale_province.get(2).region_province
                                ally3.text = dataItem.sale_province.get(2).amount+"千元"
                                alls4.text = dataItem.sale_province.get(3).region_province
                                ally4.text = dataItem.sale_province.get(3).amount+"千元"
                                alls5.text = dataItem.sale_province.get(4).region_province
                                ally5.text = dataItem.sale_province.get(4).amount+"千元"
                            }
                            if (dataItem.sale_city.size==0){
                                allc1.text = ""
                                allq1.text = ""
                                allc2.text = ""
                                allq2.text = ""
                                allc3.text = ""
                                allq3.text = ""
                                allc4.text = ""
                                allq4.text = ""
                                allc5.text = ""
                                allq5.text = ""
                            }
                            if (dataItem.sale_city.size==1){
                                allc1.text = dataItem.sale_city.get(0).region_province
                                allq1.text = dataItem.sale_city.get(0).amount+"千元"
                                allc2.text = ""
                                allq2.text = ""
                                allc3.text = ""
                                allq3.text = ""
                                allc4.text = ""
                                allq4.text = ""
                                allc5.text = ""
                                allq5.text = ""
                            }
                            if (dataItem.sale_city.size==2){
                                allc1.text = dataItem.sale_city.get(0).region_province
                                allq1.text = dataItem.sale_city.get(0).amount+"千元"
                                allc2.text = dataItem.sale_city.get(1).region_province
                                allq2.text = dataItem.sale_city.get(1).amount+"千元"
                                allc3.text = ""
                                allq3.text = ""
                                allc4.text = ""
                                allq4.text = ""
                                allc5.text = ""
                                allq5.text = ""
                            }
                            if (dataItem.sale_city.size==3){
                                allc1.text = dataItem.sale_city.get(0).region_province
                                allq1.text = dataItem.sale_city.get(0).amount+"千元"
                                allc2.text = dataItem.sale_city.get(1).region_province
                                allq2.text = dataItem.sale_city.get(1).amount+"千元"
                                allc3.text = dataItem.sale_city.get(2).region_province
                                allq3.text = dataItem.sale_city.get(2).amount+"千元"
                                allc4.text = ""
                                allq4.text = ""
                                allc5.text = ""
                                allq5.text = ""
                            }
                            if (dataItem.sale_city.size==4){
                                allc1.text = dataItem.sale_city.get(0).region_province
                                allq1.text = dataItem.sale_city.get(0).amount+"千元"
                                allc2.text = dataItem.sale_city.get(1).region_province
                                allq2.text = dataItem.sale_city.get(1).amount+"千元"
                                allc3.text = dataItem.sale_city.get(2).region_province
                                allq3.text = dataItem.sale_city.get(2).amount+"千元"
                                allc4.text = dataItem.sale_city.get(3).region_province
                                allq4.text = dataItem.sale_city.get(3).amount+"千元"
                                allc5.text = ""
                                allq5.text = ""
                            }
                            if (dataItem.sale_city.size==5){
                                allc1.text = dataItem.sale_city.get(0).region_province
                                allq1.text = dataItem.sale_city.get(0).amount+"千元"
                                allc2.text = dataItem.sale_city.get(1).region_province
                                allq2.text = dataItem.sale_city.get(1).amount+"千元"
                                allc3.text = dataItem.sale_city.get(2).region_province
                                allq3.text = dataItem.sale_city.get(2).amount+"千元"
                                allc4.text = dataItem.sale_city.get(3).region_province
                                allq4.text = dataItem.sale_city.get(3).amount+"千元"
                                allc5.text = dataItem.sale_city.get(4).region_province
                                allq5.text = dataItem.sale_city.get(4).amount+"千元"
                            }
                            //地图
                            list.clear()
                            list.addAll(result.data.sales_region)
                            shand.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "山东省") {
                                        sales_num.text = "山东省门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }
                            hen.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "河南省") {
                                        sales_num.text = "河南省门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }
                            anh.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "安徽省") {
                                        sales_num.text = "安徽省门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }
                            jiangs.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "江苏省") {
                                        sales_num.text = "江苏省门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }


                            gans.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "甘肃省") {
                                        sales_num.text = "甘肃省门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }
                            shanx.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "陕西省") {
                                        sales_num.text = "陕西省门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }
                            sanx.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "山西省") {
                                        sales_num.text = "山西省门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }
                            ningx.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "宁夏回族自治区") {
                                        sales_num.text = "宁夏回族自治区门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }
                            guangd.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "广东省") {
                                        sales_num.text = "广东省门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }
                            sheng1.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "江西省") {
                                        sales_num.text = "江西省门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }
                            sheng2.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "浙江省") {
                                        sales_num.text = "浙江省门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }
                            sheng3.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "福建省") {
                                        sales_num.text = "福建省门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }
                            sheng4.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "湖北省") {
                                        sales_num.text = "湖北省门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }
                            sheng5.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "四川省") {
                                        sales_num.text = "四川省门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }
                            sheng6.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "贵州省") {
                                        sales_num.text = "贵州省门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }
                            sheng7.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "湖南省") {
                                        sales_num.text = "湖南省门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }
                            sheng8.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "海南省") {
                                        sales_num.text = "海南省门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }
                            sheng10.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "重庆") {
                                        sales_num.text = "重庆门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }
                            sheng11.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "天津") {
                                        sales_num.text = "天津门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }
                            sheng12.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "内蒙古自治区") {
                                        sales_num.text = "内蒙古自治区门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }
                            sheng13.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "黑龙江省") {
                                        sales_num.text = "黑龙江省门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }
                            sheng14.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "吉林省") {
                                        sales_num.text = "吉林省门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }
                            sheng15.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "辽宁省") {
                                        sales_num.text = "辽宁省门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }
                            sheng16.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "河北省") {
                                        sales_num.text = "河北省门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }

                            sheng17.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "北京") {
                                        sales_num.text = "北京门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }

                            sheng18.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "青海省") {
                                        sales_num.text = "青海省门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }

                            sheng19.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "广西壮族自治区") {
                                        sales_num.text = "广西壮族自治区门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }

                            sheng20.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "云南省") {
                                        sales_num.text = "云南省门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }
                            sheng21.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "新疆维吾尔自治区") {
                                        sales_num.text = "新疆维吾尔自治区门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }
                            sheng22.setOnClickListener {
                                var i = 0
                                while (i < list.size) {
                                    if (list[i].pname == "西藏自治区") {
                                        sales_num.text = "西藏自治区门店数："
                                        sales_num1.text = list[i].nums
                                    }
                                    i++
                                }
                            }



                        } else {
                            ResultCheckUtils().checkResult(mContext, result.code, result.msg)
                        }
                    }
                    ) { e: Throwable -> LogUtils.showLog(HttpManager.TAG, e.message) }
        }

    private fun initRecyclerViewCurrent(list: ArrayList<AllBean.DataBean.SaleDetailBean>) {
        rec_all.layoutManager = LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false)
        rec_all.adapter = RVAdapterAreaCurrentAll(mContext, list)
        rec_all.start()
    }

}